package com.ybkj.daijia.server.controller;

import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.server.model.RentInsurance;
import com.ybkj.daijia.server.service.RentInsuranceService;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping(value = "rentinsurance")
public class RentInsuranceController {

    @Autowired
    private RentInsuranceService rentInsuranceService;

    /**
     * 以下是2017年4月13号最新的 liyunhao
     */
    @ResponseBody
    @RequestMapping(value = "findRentInsuranceByCarId", method = RequestMethod.GET)
    public Page<RentInsurance> findInsuranceByCarId(
        @RequestParam(required = false, value = "page") Integer page,
        @RequestParam(required = false, value = "rows") Integer rows,
        @RequestParam(required = false, value = "carId") Long carId,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        return rentInsuranceService.findInsuranceByCarId(page, rows, carId);


    }


    @ResponseBody
    @RequestMapping(value = "findRentInsuranceById", method = RequestMethod.GET)
    public RentInsurance findInsuranceById(
        @RequestParam(value = "id") Long id,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        return rentInsuranceService.findInsuranceById(id);

    }


    @RequestMapping(value = "updateRentInsurance", method = RequestMethod.POST)
    public Map<String, Object> updateInsurance(
        @RequestParam(required = false, value = "company") String company,
        @RequestParam(required = false, value = "insuranceNo") String insuranceNo,
        @RequestParam(required = false, value = "insuranceType") String insuranceType,
        @RequestParam(required = false, value = "money") Double money,
        @RequestParam(required = false, value = "validDate") Integer validDate,
        @RequestParam(required = false, value = "invalidDate") Integer invalidDate,
        HttpServletResponse response) throws Exception {
        response.setHeader("Access-Control-Allow-Origin", "*");
        return rentInsuranceService
            .updateInsurance(company, insuranceNo, insuranceType, money, validDate, invalidDate);

    }


    @RequestMapping(value = "deleteRentInsuranceByCarId", method = RequestMethod.POST)
    public Map<String, Object> deleteInsuranceByCarId(
        @RequestParam(value = "carId") Long carId,
        HttpServletResponse response) throws Exception {
        response.setHeader("Access-Control-Allow-Origin", "*");
        return rentInsuranceService.deleteInsuranceByCarId(carId);

    }


    @RequestMapping(value = "insertRentInsurance", method = RequestMethod.POST)
    public Map<String, Object> insertInforMation(
        @RequestParam(required = false, value = "carId") Long carId,
        @RequestParam(required = false, value = "company") String company,
        @RequestParam(required = false, value = "insuranceNo") String insuranceNo,
        @RequestParam(required = false, value = "insuranceType") String insuranceType,
        @RequestParam(required = false, value = "money") Double money,
        @RequestParam(required = false, value = "validDate") Integer validDate,
        @RequestParam(required = false, value = "invalidDate") Integer invalidDate)
        throws Exception {

        return rentInsuranceService
            .insertInsurance(carId, company, insuranceNo, insuranceType, money, validDate,
                invalidDate);
    }

}
